#include "StdAfx.h"
#include "akmodel.h"


/// \class AKModel
/// \brief
///
///
///
AKModel::AKModel(QObject *parent)
	: QAbstractListModel(parent)
{
	QHash<int,QByteArray> roles;
	roles[PathRole] = "path";
	roles[ThumbnailRole] = "thumbnail";
	roles[TrainRole] = "train";
	setRoleNames(roles);

	initialize();
}

AKModel::~AKModel()
{
}

//////////////////////////////////////////////////////////////////////////

int AKModel::rowCount(const QModelIndex &parent) const
{
//	return mList.size();
	return 0;
}

QVariant AKModel::data(const QModelIndex &index, int role) const
{
//	//if (!index.isValid())
//	//	return QVariant();
//
//	if (index.row() < 0 || index.row() > mList.size())
//		return QVariant();
//
//	const SoundItem &soundItem = mList[index.row()];
//
//	switch (role)
//	{
//	case Qt::DisplayRole:
//		return soundItem.thumbnail();
//	case PathRole:
//		return soundItem.path();
//	case ThumbnailRole:
//		return soundItem.thumbnail();
//	case TrainRole:
//		return soundItem.train();
//	//case Qt::DecorationRole:
//	//case Qt::EditRole:
//	//case Qt::ToolTipRole:
//	//case Qt::StatusTipRole:
//	//case Qt::WhatsThisRole:
//	//case Qt::SizeHintRole:
//	//...
//	}
//
//	return QVariant();
	return QVariant();
}

//////////////////////////////////////////////////////////////////////////

void AKModel::initialize()
{
	mMediaObject = new Phonon::MediaObject(this);
	mAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
	mMediaSource = new Phonon::MediaSource();
}

//////////////////////////////////////////////////////////////////////////

/// \brief
///
///
///
/// \return Nothing
/// \sa
void AKModel::load(QStringList paths)
{


}

void AKModel::load(QString path)
{


	// read file path
	QFile file(path);
	file.open(QIODevice::ReadOnly);

	// read and save raw file data & create the hash
	QByteArray rawData(file.readAll());
	QByteArray hash = QCryptographicHash::hash(rawData,QCryptographicHash::Md5);
	


	// buffer the raw data
	QBuffer *buffer = new QBuffer(mMediaObject);
	buffer->setData(rawData);


	mMediaSource = new Phonon::MediaSource(buffer);
	currentPath= new QString(path);
	SoundItem soundItem(path, rawData);

	mData.insert(rawData, rawData);

	mMediaObject = Phonon::createPlayer( Phonon::MusicCategory );
	
	SoundItem c;
	c.sound_data() = mData.value(rawData);

	buffer->setData(c.sound_data());

	mMediaObject->setCurrentSource(buffer);
	
	mMediaObject->play();

	
	////QString filename = QFileDialog::getSaveFileName(parent,"Datei Speichern","","Text Files(*.txt)");
	//QString filename = "borsti";
	//if(filename != ""){
	//	QFile file(filename);
	//	if(!file.open(QIODevice::WriteOnly)){
	//		
	//		return;
	//	}else{
	//		QTextStream stream(&file);
	//		for(int i=0; i!=mData.keys().count();i++){
	//			QList<QByteArray> keyList(mData.keys());
	//			QList<QByteArray> dataList(mData.values());
	//			QByteArray *data= new QByteArray(dataList[i]);
	//			QString *saveText = new QString(*data);
	//			stream << saveText;
	//		}
	//		
	//		
	//		stream.flush();
	//		file.close();
	//	}
	//}

	
}




/// \brief Set
///
///
///
/// \return Nothing
/// \sa
void AKModel::set(QByteArray hash)
{
	mCurrentItem = &get(hash);
}

/// \brief
///
///
///
/// \return Nothing
/// \sa
void AKModel::set(QString user, QString category, int index)
{
	set(isValid(user,category,index).at(index));
}

/// \brief
///
///
///
/// \return SoundItem
/// \sa
SoundItem AKModel::get(QByteArray hash)
{
	//return mData.find(hash).value();
	return SoundItem();
}

/// \brief
///
///
///
/// \return SoundItem
/// \sa
SoundItem AKModel::get(QString user, QString category, int index)
{
	
	return get(isValid(user,category,index).at(index));
}

/// \brief
///
///
///
/// \return CategoryData
/// \sa
CategoryData AKModel::isValid(QString user, QString category, int index)
{
	if (!user.isEmpty()) {mCurrentUser = user;}
	if (!category.isEmpty()) {mCurrentCategory = category;}

	CategoryData current = mOrganization.find(mCurrentUser).value().find(mCurrentCategory).value();
	if (index > 0 && index < current.size()) {mCurrentIndex = index;}

	return current;
}

//////////////////////////////////////////////////////////////////////////

//QModelIndex ProfileModel::parent(const QModelIndex &child) const
//{
//	return child;
//}

//int ProfileModel::columnCount(const QModelIndex &parent) const
//{
//	return 0;
//}

//bool ProfileModel::hasChildren(const QModelIndex &parent) const
//{
//	return true;
//}